package system;

import java.io.*;
import java.util.*;

/**
 * @author Sean Freitag
 *
 * Keeps detailed logs for diagnostic puposes
 */
public class Log {
	
	/**
	 * Log file
	 */
	private File file;
	
	/**
	 * True if this is the server log
	 * if True, all log entries will be printed to the console
	 */
	private boolean isPrimary = false;
	
	/**
	 * Creates a new Log
	 * @param name
	 * Name of the log, typically the username or server
	 */
	public Log(String name){
		file = new File(name + ".log");
	}
	
	/**
	 * Creates a new Log, usually for a Server
	 * @param name
	 * Name of the log
	 * @param isPrimary
	 * True if log entries should be printed to the screen
	 */
	public Log(String name, boolean isPrimary){
		this(name);
		this.isPrimary = isPrimary;
	}

	/**
	 * Writes a line to the log
	 * @param string
	 * Line to be written
	 */
	public void write(String string){

			try {
				//apends file
				FileWriter writer = new FileWriter(file, true);
				string = new Date(System.currentTimeMillis()).toString() + " | " + string;
				
				writer.append(string + "\n");
				if(isPrimary)
					System.out.println(string);
				
				writer.close();
				
			} catch (Exception e) {
				//This should not happen, but if it does, we should know why
				e.printStackTrace();
			}
		
	}

	
}
